<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/29
 * Time: 下午8:29
 */

class CoinService extends Service {

    public static function indexHeader(){
        $getHeader = array(
            'RecDate'   => array('text' => PubFunc::t('日期')),
            'Action'   => array('text' => PubFunc::t('产出点')),
            'GetCoin'   => array('text' => PubFunc::t('产出货币'),'rule'=>'numeric'),
            'GetUser'   => array('text' => PubFunc::t('人数'),'rule'=>'numeric'),
            'GetCnt'   => array('text' => PubFunc::t('人次'),'rule'=>'numeric'),
            'GetRate'   => array('text' => PubFunc::t('比例'),'rule'=>'numeric'),
        );
        $useHeader = array(
            'RecDate'   => array('text' => PubFunc::t('日期')),
            'Action'   => array('text' => PubFunc::t('消耗点')),
            'UsedCoin'   => array('text' => PubFunc::t('消耗货币'),'rule'=>'numeric'),
            'UsedUser'   => array('text' => PubFunc::t('人数'),'rule'=>'numeric'),
            'UsedCnt'   => array('text' => PubFunc::t('人次'),'rule'=>'numeric'),
            'UsedRate'   => array('text' => PubFunc::t('比例'),'rule'=>'numeric'),
        );

        return array(
            'get'   =>  $getHeader,
            'used'  =>  $useHeader
        );
    }

    public static function exportHeader(){
        $header = array(
            'RecDate'   => array('text' => '日期'),
            'Type'      => array('text' => '产出/消耗'),
            'Action'    => array('text' => '功能点'),
            'Coin'      => array('text' => '相关货币','rule'=>'numeric'),
            'User'      => array('text' => '相关人数','rule'=>'numeric'),
            'Cnt'       => array('text' => '相关人次','rule'=>'numeric'),
            'Rate'      => array('text' => '相关比例','rule'=>'numeric'),
        );

        return $header;
    }

    public static function indexData($params,$dict = array()){
    	if($params['time_start'] == $params['time_end']){
    		$date = $params['time_start'];
    	}else{
    		$date = date('Y.m.d', strtotime($params['time_start'])).'~'.date('Y.m.d', strtotime($params['time_end']));
    	}

        $dict['Action'] = PubDict::dict($params['g'], 'waytype');

        $data = ModCoin::indexData($params);
        $allGetTotal = $allUsedTotal = $residue = 0;
        foreach($data as &$item){
        	$item['RecDate']           = $date;
        	$waytype = explode('_', $item['Action']);
        	$item['Action'] = $waytype[1];
        	
        	$allGetTotal += $item['GetCoin'];
        	$allUsedTotal += $item['UsedCoin'];
        }
        $residue = $allGetTotal - abs($allUsedTotal);
        unset($item);

        PubFunc::parseDict($data,$dict);
        foreach($data as &$item){
        	$item['Action'] = $item['CoinType'].'__'.$item['Action'];
        }
        unset($item);
        
        $data = self::splitData($data);
        
        $allParamsGet = array(
            "total" => array("RecDate"=>"Total"),
            "sumKey"=>array("GetCoin")
        );
        $getRateConfig = array(
            "GetCoin"   =>  array("to" =>  "GetRate",$allParamsGet),
        );

        $allParamsUsed = array(
            "total" => array("RecDate"=>"Total"),
            "sumKey"=>array("UsedCoin")
        );
        $usedRateConfig = array(
            "UsedCoin"   =>  array("to" =>  "UsedRate",$allParamsGet),
        );

        PubStat::setRate($data['get'],$getRateConfig,$allParamsGet,$getTotal);
        PubStat::setRate($data['used'],$usedRateConfig,$allParamsUsed,$usedTotal);
        
        $data['getTotal'] = $getTotal;
        $data['usedTotal'] = $usedTotal;
        $data['allGetTotal'] = $allGetTotal;
        $data['allUsedTotal'] = $allUsedTotal;
        $data['residue'] = $residue;
        
        return $data;
    }

    public static function indexChart($data, $params){
        $chartDataGet = $chartDataUsed = array();
//         $coinTypes = PubDict::getDict($params['g'], 'CoinType');
//         $coinType = $coinTypes[$params['coinType']];

        $getLabel = PubFunc::t($coinType . '产出分布');
        $usedLabel = PubFunc::t($coinType . '消耗分布');

        $pubSort = new PubMultiSort();
        $pubSort->setSort('GetCoin','desc');
        $data['get'] = $pubSort->getResult($data['get']);

        $pubSort = new PubMultiSort();
        $pubSort->setSort('UsedCoin','desc');
        $data['used'] = $pubSort->getResult($data['used']);

        foreach($data['get'] as $v){
            $chartDataGet[$getLabel][PubFunc::t($v['Action'])] = intval($v['GetCoin']);
        }

        foreach($data['used'] as $v){
            $chartDataUsed[$usedLabel][PubFunc::t($v['Action'])] = intval($v['UsedCoin']);
        }



        return array(
            'get'=> array(
                'data'   => $chartDataGet
            ),
            'used'=> array(
                'data'   => $chartDataUsed
            ),
        );
    }

    public static function splitData($data){
        $get = array();
        $used = array();
        foreach($data as $v){
            if($v['UsedCnt']){
                $used[] = $v;
            }else{
                $get[] = $v;
            }
        }

        return array(
            "get"   =>  $get,
            "used"  =>  $used
        );
    }

    public static function getCoinType(){
        return PubDict::dict('mango', 'currencytype');
    }


    public static function exportRankingHeader(){
        $header = array(
            'Type'      => array('text' => '消费/获得'),
            'No'        => array('text' => '排行'),
            'RoleID'    => array('text' => '角色ID'),
            'RoleName'  => array('text' => '角色名'),
            'GetNumber' => array('text' => '相关货币'),
        );

        return $header;
    }

    /**
     * 富豪榜表头
     * @return array
     */
    public static function rankingHeader(){
        $getHeader = array(
            'No'        => array('text' => '排行'),
            'RoleID'    => array('text' => '角色ID'),
            'RoleName'  => array('text' => '角色名'),
            'GetNumber' => array('text' => '获得货币','rule'=>'numeric'),
        );
        $useHeader = array(
            'No'        => array('text' => '排行'),
            'RoleID'    => array('text' => '角色ID'),
            'RoleName'  => array('text' => '角色名'),
            'GetNumber' => array('text' => '消费货币','rule'=>'numeric'),
        );

        return array(
            'get'   =>  $getHeader,
            'used'  =>  $useHeader
        );
    }

    /**
     * 富豪榜数据
     */
    public static function rankingData($params){
        $tmp = ModCoin::rankingData($params);
        $result = array(
            'used'  => array(),
            'get'   => array()
        );

        $userArr = array();
        foreach($tmp as $v){
            $userArr[$v['RoleID']] = $v['RoleID'];
        }
//        $dict = LogRoleService::getDictByUser($userArr);

        $index   = array();
        foreach($tmp as $v){
            if($v['ActionType'] == '1'){
                $type = 'used';
            }else{
                $type = 'get';
            }

            if(isset($index[$type])){
                $index[$type]++;
            }else{
                $index[$type] = 1;
            }
            $v['No'] = $index[$type];

//            if(isset($dict[$v['RoleID']])){
//                $v['RoleName'] = $dict[$v['RoleID']]['name'].' <a target="_blank" href="/oss/?r=role/index&m=36&g=roma&server='.$dict[$v['RoleID']]['server'].'&search='.$v['RoleID'].'"><i class="fa fa-info-circle"></i></a>';
//            }



            $result[$type][] = $v;
        }


        return $result;
    }


    /**
     * 富豪榜数据
     */
    public static function cumRankingData($params){
        $tmp = ModCoin::cumRankingData($params);
        $result = array(
            'used'  => array(),
            'get'   => array()
        );

        $userArr = array();
        foreach($tmp as $v){
            $userArr[$v['RoleID']] = $v['RoleID'];
        }
//        $dict = LogRoleService::getDictByUser($userArr);

        $index   = array();
        foreach($tmp as $v){
            if($v['ActionType'] == '1'){
                $type = 'used';
            }else{
                $type = 'get';
            }

            if(isset($index[$type])){
                $index[$type]++;
            }else{
                $index[$type] = 1;
            }
            $v['No'] = $index[$type];

//            if(isset($dict[$v['RoleID']])){
//                $v['RoleName'] = $dict[$v['RoleID']]['name'].' <a target="_blank" href="/oss/?r=role/index&m=36&g=roma&server='.$dict[$v['RoleID']]['server'].'&search='.$v['RoleID'].'"><i class="fa fa-info-circle"></i></a>';
//            }



            $result[$type][] = $v;
        }


        return $result;
    }

} 